package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : EaringDetailFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:47
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.EarningDetailAdapter;
import com.chanjiedata.changbs.base.BaseFragment;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Recharge;
import com.chanjiedata.changbs.utlis.DateUtil;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by AndroidIntexh1 on 2018/6/4.
 */

public class EaringDetailFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.fragment_recycleView)
    RecyclerView fragmentRecycleView;


    private int page = 1; // 当前页数
    private int position;  // 当前viewPage的位置 0:提现消息 1:奖励消息 2:担保消息 3:系统消息

    private List<Recharge.Earnings_detail_list.Earnings_detail_array_list> datas = new ArrayList<>(); // 提现消息
    private EarningDetailAdapter txAdapter; // 提现消息适配器
    private String type;
    private String searchTime = DateUtil.getYear() + "-" + DateUtil.getMonth();
    private int pagesize = 10;

    public static EaringDetailFragment NewInstance(int instanceType) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        EaringDetailFragment messageTypeFragment = new EaringDetailFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();
        loadData();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        type = "0";
        switch (position) {
            case 0:
                type = "merchant";
                break;
            case 1:
                type = "partner";
                break;
            case 2:
                type = "activation";
                break;
            case 3:
                type = "cash_pay";
                break;
            case 4:
                type = "lineCard_full";
                break;
            case 5:
                type = "other";
                break;
        }
        Recharge.Earnings_detail_list.Builder builder = Recharge.Earnings_detail_list.newBuilder();
        builder.setType(type).setSearchMonth(searchTime);
        NetworkManager.INSTANCE.post(Apis.pd_cash_list, builder.build().toByteArray(), page, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                List<Recharge.Earnings_detail_list.Earnings_detail_array_list> lists =
                        Recharge.Earnings_detail_list.parseFrom(response).getEarningsDetailArrayListList();
                txAdapter.addData(lists);
                int size = lists.size();
                if (size < 10) {
                    txAdapter.loadMoreEnd(true);
                } else {
                    txAdapter.loadMoreComplete();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });

    }


    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        fragmentRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        fragmentRecycleView.setAdapter(txAdapter = new EarningDetailAdapter(mContext, datas));
        // 每个item的点击事件
        txAdapter.bindToRecyclerView(fragmentRecycleView);
        txAdapter.setEmptyView(R.layout.recycle_empty);
        txAdapter.setOnLoadMoreListener(() -> {
            page = txAdapter.getData().size() / pagesize + 1;
            loadData();
        }, fragmentRecycleView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    public void startReflash(String searchTime) {
        page = 1;
        this.searchTime = searchTime;
        datas.clear();
        Recharge.Earnings_detail_list.Builder builder = Recharge.Earnings_detail_list.newBuilder();
        builder.setType(type).setSearchMonth(this.searchTime);
        NetworkManager.INSTANCE.post(Apis.pd_cash_list, builder.build().toByteArray(), page, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                List<Recharge.Earnings_detail_list.Earnings_detail_array_list> lists =
                        Recharge.Earnings_detail_list.parseFrom(response).getEarningsDetailArrayListList();
                datas.addAll(lists);
                txAdapter.setNewData(datas);
                int size = lists.size();
                if (size < 10) {
                    txAdapter.loadMoreEnd(true);
                } else {
                    txAdapter.loadMoreComplete();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });

    }
}
